import json
import requests

from logger import log

"""
{
    "equipment_type": "XK3190",
    "com_path": "COM1",
    "com_baud_rate": 9600
}
"""


class Settings:
    def __init__(self):
        self.platformName = None
        self.platformId = None
        self.name = None
        self.code = None
        self.id = None
        self.host = None
        self.type = None
        # 称重设备相关配置
        self.equipment_type = None
        self.com_path = None
        self.com_baud_rate = None
        self.weight_wen_timeout = None
        self.mqtt_server = None
        self.mqtt_username = None
        self.mqtt_password = None
        self.multiplier = None
        # 温度设备相关配置
        self.temperatureList = None
        # 门禁相关配置
        self.access_control_ip = None
        self.access_control_username = None
        self.access_control_password = None
        # modbus相关配置
        self.vm1_server = None
        self.vm2_server = None
        self.modbus_list = None
        self.server_url = None
        self.directory = None

    def init(self, code="test", host="platform.wechain360.com", local=False):
        if not local:
            self.host = host
            self.code = code

            # 先执行一次再进入循环；
            url = f"http://{host}/api/weigh/weigh/getLocalConfig/{code}"
            response = requests.get(url)
            ctx = response.text
            loads = json.loads(ctx)
            log.info(f"config response:{loads}")
            self.id = loads['id']
            self.name = loads['name']
            self.platformId = loads['platformId']
            config_json = loads['extSettings']

            self.equipment_type = config_json.get('equipment_type', "XK3190")
            self.com_path = config_json.get('com_path', 'COM1')
            self.com_baud_rate = config_json.get('com_baud_rate', 9600)
            self.weight_wen_timeout = config_json.get('weight_wen_timeout', 3)
            self.mqtt_server = config_json.get('mqtt_server', 'mq.wechain360.com')
            self.mqtt_username = config_json.get('mqtt_username', 'client1')
            self.mqtt_password = config_json.get('mqtt_password', 'Hlsk@1234')
            self.multiplier = config_json.get('multiplier', 1000.0)
        else:
            with open('config.json', 'r', encoding='utf-8') as f:
                config_json = json.load(f)
                self.code = config_json.get('code', "test")
                self.host = config_json.get('host', "platform.wechain360.com")
                self.platformId = config_json.get('platformId', "test")
                self.platformName = config_json.get('platformName', "test")
                self.type = config_json.get('type', "temperature")
                self.mqtt_server = config_json.get('mqtt_server', 'mq.wechain360.com')
                self.mqtt_username = config_json.get('mqtt_username', 'client1')
                self.mqtt_password = config_json.get('mqtt_password', 'Hlsk@1234')
                self.temperatureList = config_json.get('list', [])
                self.access_control_ip = config_json.get('access_control_ip', "192.168.1.100")
                self.access_control_username = config_json.get('access_control_username', "admin")
                self.access_control_password = config_json.get('access_control_password', "hlsk1521")
                self.vm1_server = config_json.get('vm1_server', None)
                self.vm2_server = config_json.get('vm2_server', None)
                self.modbus_list = config_json.get('modbus_list', [])
                self.server_url = config_json.get('server_url', None)
                self.directory = config_json.get('directory', None)


settings = Settings()

if __name__ == '__main__':
    settings.init()
    print(settings)
